package cn.org.itool.ws.framework.comment;

import cn.hutool.core.collection.CollectionUtil;
import cn.org.itool.ws.framework.netty.message.BizWsMessage;
import cn.org.itool.ws.framework.ws.WsServer;
import cn.org.itool.ws.pojo.BroadcastCloseMessage;
import cn.org.itool.ws.pojo.BroadcastMessage;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;

@Log4j2
@Component
public class BroadcastComment {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 推送消息
     * @param groupName 组名
     * @param body 消息
     */
    public void push(String groupName, BizWsMessage body) {
        BroadcastMessage message = new BroadcastMessage();
        message.setGroupName(groupName);
        message.setBody(body);
        try {

            String jsonString = WsServer.OBJECT_MAPPER.writeValueAsString(message);
            stringRedisTemplate.convertAndSend(BroadcastRegisterComment.BROADCAST_TOPIC, jsonString);
        }catch (Exception e) {
            log.error("push error :", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 推送消息
     * @param groupName 组名
     * @param body 消息
     */
    public void push(String groupName, List<String> includeUserIds, BizWsMessage body) {
        BroadcastMessage message = new BroadcastMessage();
        message.setGroupName(groupName);
        message.setBody(body);
        message.setIncludeUserIds(includeUserIds);
        try {

            String jsonString = WsServer.OBJECT_MAPPER.writeValueAsString(message);
            stringRedisTemplate.convertAndSend(BroadcastRegisterComment.BROADCAST_TOPIC, jsonString);
        }catch (Exception e) {
            log.error("push error :", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 推送消息
     * @param groupName 组名
     * @param body 消息
     */
    public void push(String groupName, List<String> includeUserIds, List<String> ignoreUserIds, BizWsMessage body) {
        BroadcastMessage message = new BroadcastMessage();
        message.setGroupName(groupName);
        message.setBody(body);
        message.setIncludeUserIds(includeUserIds);
        message.setIgnoreUserIds(ignoreUserIds);
        try {

            String jsonString = WsServer.OBJECT_MAPPER.writeValueAsString(message);
            stringRedisTemplate.convertAndSend(BroadcastRegisterComment.BROADCAST_TOPIC, jsonString);
        }catch (Exception e) {
            log.error("push error :", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 关闭
     * @param wsIds channel id
     */
    public void close(Set<String> wsIds) {
        if(CollectionUtil.isEmpty(wsIds)) {
            return;
        }
        try {
            BroadcastCloseMessage vo = new BroadcastCloseMessage();
            vo.setWsIds(wsIds);
            String jsonString = WsServer.OBJECT_MAPPER.writeValueAsString(vo);
            stringRedisTemplate.convertAndSend(BroadcastRegisterComment.BROADCAST_CLOSE_TOPIC, jsonString);
        }catch (Exception e) {
            log.error("e:", e);
        }
    }
}
